package com.cmict.data.infra.dal.mysql.consulthistory;

import java.time.LocalDateTime;
import java.util.*;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cmict.data.framework.mybatis.core.mapper.BaseMapperX;
import com.cmict.data.infra.dal.dataobject.consulthistory.ConsultHistoryDO;
import org.apache.ibatis.annotations.Mapper;
import com.cmict.data.infra.controller.admin.consulthistory.vo.*;
import org.hibernate.validator.constraints.Length;

/**
 * 咨询历史管理 Mapper
 *
 * @author 系统管理员
 */
@Mapper
public interface ConsultHistoryMapper extends BaseMapperX<ConsultHistoryDO> {

    default PageResult<ConsultHistoryDO> selectPage(ConsultHistoryPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ConsultHistoryDO>()
                .eqIfPresent(ConsultHistoryDO::getChatContent, reqVO.getChatContent())
                .eqIfPresent(ConsultHistoryDO::getFromUserId, reqVO.getFromUserId())
                .eqIfPresent(ConsultHistoryDO::getToTypeId, reqVO.getToTypeId())
                .betweenIfPresent(ConsultHistoryDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(ConsultHistoryDO::getId));
    }

    default List<ConsultHistoryDO> selectList(ConsultHistoryExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ConsultHistoryDO>()
                .eqIfPresent(ConsultHistoryDO::getChatContent, reqVO.getChatContent())
                .eqIfPresent(ConsultHistoryDO::getFromUserId, reqVO.getFromUserId())
                .eqIfPresent(ConsultHistoryDO::getToTypeId, reqVO.getToTypeId())
                .betweenIfPresent(ConsultHistoryDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(ConsultHistoryDO::getId));
    }

    default  List<ConsultHistoryDO> getChatHistoryList(ConsultHistoryCreateReqVO qo){
//        QueryWrapper<ConsultHistoryDO> queryWrapper = new QueryWrapper<>();
//        if (qo.getFromUserId() != null) {
//            queryWrapper.lambda().and(qw -> qw.like(ConsultHistoryDO::getFromUserId, qo.getFromUserId())
//                    .or().like(ConsultHistoryDO::getToTypeId, qo.getToTypeId()));
//        }
//        if (qo.getToTypeId() != null) {
//            queryWrapper.lambda().and(qw -> qw.like(ConsultHistoryDO::getFromUserId, qo.getFromUserId())
//                    .or().like(ConsultHistoryDO::getToTypeId, qo.getToTypeId()));
//        }
//        queryWrapper.lambda().between(ConsultHistoryDO::getCreateTime, LocalDateTimeUtil.beginOfDay(LocalDateTime.now()), LocalDateTimeUtil.endOfDay(LocalDateTime.now()));
//        queryWrapper.lambda().orderByAsc(ConsultHistoryDO::getCreateTime);
//       return selectList(queryWrapper);

        return selectList(new LambdaQueryWrapperX<ConsultHistoryDO>()
                .eq(ConsultHistoryDO::getFromUserId, qo.getFromUserId())
                .or()
                .eq(ConsultHistoryDO::getFromUserId,qo.getToTypeId())
                .eq(ConsultHistoryDO::getToTypeId,qo.getFromUserId())
                .or()
                .eq(ConsultHistoryDO::getToTypeId,qo.getToTypeId())
                .between(ConsultHistoryDO::getCreateTime, LocalDateTimeUtil.beginOfDay(LocalDateTime.now()), LocalDateTimeUtil.endOfDay(LocalDateTime.now()))
                .orderByAsc(ConsultHistoryDO::getCreateTime));
    }


}
